package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class MainBarServlet extends HttpServlet {
    private EJBDatabaseQuery ejbdb;

    public void doPost(HttpServletRequest request,  HttpServletResponse response) 
    {
	try {
	    response.setContentType("text/html");
	    HttpSession session = request.getSession(true);
	    PrintWriter out  = response.getWriter();		  
	    String user_ip   = request.getRemoteAddr();
	    String call_path = request.getContextPath()+request.getServletPath();

	    out.println(MyXml.getComments("Main"));

	    String mid = (String)request.getAttribute("mid");
	    if(mid == null) {
		System.out.println("MainBarServlet:Error:[mid==null]");
		return;
	    }

	    if(mid.equals("check_mail")) {
		try {
		    RequestDispatcher rd = request.getRequestDispatcher("AdvServlet");
		    rd.include(request,response);
		}
		catch (IOException e) {
		    System.out.println("MainBarServlet:Error:"+e);
		    return;
		}
		catch (ServletException e) {
		    System.out.println("MainBarServlet:Error:"+e);
		    return;
		}
		return;
	    }

	    //////////////////////////////////////////////////////////////////////////
	    // SideBar
	    //////////////////////////////////////////////////////////////////////////
	    Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	    
	    if(dir_hs == null) {
		System.out.println("MainBarServlet:Error:103:[dir_hs==null]");
		return;
	    }

	    ///////////////////////////////////////////////////////////////////////////
	    // user info
	    ///////////////////////////////////////////////////////////////////////////
	    String user_name  = null;
	    String user_fname = null;
	    String user_since = null;
	    String user_edu   = null;
	    String user_zip   = null;
	    String user_sex   = null;

	    int user_age      = 0;
	    int user_level    = 0;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info != null) {
		user_name  = (String)user_info.get("user_name");
		user_fname = (String)user_info.get("user_fname");
		user_since = (String)user_info.get("user_since");
		user_edu   = (String)user_info.get("user_edu");
		user_zip   = (String)user_info.get("user_zip");
		user_sex   = (String)user_info.get("user_sex");
		user_age   = Integer.parseInt((String)user_info.get("user_age"));
		user_level = Integer.parseInt((String)user_info.get("user_level"));
	    }
	    else {
		System.out.println("MainBarServlet:Error:104");
		return;
	    }

	    String sid = (String)dir_hs.get(mid+"_sid");

	    //===============================================================
	    // user image guest view
	    //===============================================================
	    
	    if(sid == null) {
		UserHomeBean uhbean = new UserHomeBean();
		out.println(uhbean.getGuestImageList(1280, 1024, request, session, mid));
		return;
	    }
	    
	    String type          = null;
	    String mlink         = null;
	    String managers      = null;
	    String admind        = null;
	    String name          = null;

	    int menu_level       = 99;
	    int menu_age         = 99;
	    int preview_level    = 99;
	    int preview_age      = 99;
	    int read_level       = 99;
	    int read_age         = 99;
	    int post_level       = 99;
	    int post_age         = 99;
	    int edit_level       = 99;
	    int remove_level     = 99;
	    int delete_level     = 99;
	    int admin_level      = 99;
	    
	    try {
		type          = (String)dir_hs.get(mid+"_type");
		mlink         = (String)dir_hs.get(mid+"_mlink");
		managers      = (String)dir_hs.get(mid+"_managers");
		admind        = (String)dir_hs.get(mid+"_admins");
		name          = (String)dir_hs.get(mid+"_name");

		menu_level       = Integer.parseInt((String)dir_hs.get(mid+"_menu_level"));
		menu_age         = Integer.parseInt((String)dir_hs.get(mid+"_menu_age"));
		preview_level    = Integer.parseInt((String)dir_hs.get(mid+"_preview_level"));
		preview_age      = Integer.parseInt((String)dir_hs.get(mid+"_preview_age"));
		read_level       = Integer.parseInt((String)dir_hs.get(mid+"_read_level"));
		read_age         = Integer.parseInt((String)dir_hs.get(mid+"_read_age"));
		post_level       = Integer.parseInt((String)dir_hs.get(mid+"_post_level"));
		post_age         = Integer.parseInt((String)dir_hs.get(mid+"_post_age"));
		edit_level       = Integer.parseInt((String)dir_hs.get(mid+"_edit_level"));
		remove_level     = Integer.parseInt((String)dir_hs.get(mid+"_remove_level"));
		delete_level     = Integer.parseInt((String)dir_hs.get(mid+"_delete_level"));
		admin_level      = Integer.parseInt((String)dir_hs.get(mid+"_admin_level"));
	    }
	    catch (Exception e) {
		System.out.println("Error:MainBarServlet:101:"+e);
	    }

	    boolean preview = false;
	    boolean manager = false;
	    boolean admin   = false;

	    if(type.equals("1") || type.equals("0") || type.equals("4")) {
		String aid = request.getParameter("aid");
		//aid == 0 or null preview
		//aid == 1         read (detail)
		//aid == 2         write (add)
		//aid == 3         manager 
		//aid == 4         admin

		if(menu_level > user_level) {
		    System.out.println("menu_level="+menu_level+"user_level="+user_level);
		    return;
		}

		if(menu_age > user_age) {
		    System.out.println("menu_age="+menu_age+"user_age="+user_age);
		    return;
		}

		Hashtable achs = new Hashtable();

		achs.put("read_level", ""+read_level);
		achs.put("read_age",   ""+read_age);
		achs.put("post_level", ""+post_level);
		achs.put("post_age",   ""+post_age);

		if(ServletBean.isManager(managers, user_name, ";")) {
		    achs.put("show_manager", "true");
		    manager = true;
		}

		if(user_level>=admin_level) {
		    achs.put("show_admin", "true");
		    admin = true;
		}
		
		ShowAdv adv  = new ShowAdv();

		String left  = (adv.getAdv(sid, "2")).toString();
		String right = (adv.getAdv(sid, "3")).toString();
		
		out.println("<table width=100% height=100%>");
		out.println("<tr>");
	       
		if(mid.equals("23")) { // user home photo
		    out.println("<td align=center valign=top style='BORDER: 1px solid #999999;' width=130>");
		    UserHomeBean uhbean = new UserHomeBean();
		    if(user_level>0) {
			out.println("<BR>");
			out.println(uhbean.getActionBar(call_path, mid));
			out.println(uhbean.getUserFolders(call_path, mid, name, user_name));
		    }
		    out.println("</td>");
		}

		/*
		else {
		    out.println("<td align=center valign=top style='BORDER: 1px solid #999999;' width=130>");
		    out.println(left);
		    out.println("</td>");
		}
		*/

		out.println("<td align=center valign=top style='BORDER: 0px solid #999999;'>");

		boolean show_mlink = true;

		if(aid != null) {
		    if(aid.startsWith("0")) {
			show_mlink = true;
		    }
		    else if(aid.startsWith("1")) {
			if(read_level > user_level) {
			    out.println(MyJavaScript.disableHistory());
			    StringBuffer sbf = (new TopBarBean()).getLoginForm(false, session.getId(), call_path, user_ip);
			    session.setAttribute("count", "0");
			    out.println("<BR><BR>"+MyMessage.tableMessage("Access Level ["+read_level+"] is required.",12));
			    out.println("<BR><BR>"+sbf);
			    out.println("<BR><A href="+call_path+"?mid=72>Join Us</A>&nbsp;(Free)&nbsp;&nbsp;");
			    out.println("<A href="+call_path+"?tid=forgot_pass>Forgot Password?</A>");
			    show_mlink = false;
			}
		    }
		    else if(aid.startsWith("2")) {
			if(post_level > user_level) {
			    out.println(MyJavaScript.disableHistory());
			    StringBuffer sbf = (new TopBarBean()).getLoginForm(false, session.getId(), call_path, user_ip);
			    session.setAttribute("count", "0");
			    out.println("<BR><BR>"+MyMessage.tableMessage("Access Level ["+post_level+"] is required.",12));
			    out.println("<BR><BR>"+sbf);
			    show_mlink = false;
			}
		    }
		    else if(aid.startsWith("3")) {
			if(!(manager || admin)) {
			    out.println(MyJavaScript.disableHistory());
			    StringBuffer sbf = (new TopBarBean()).getLoginForm(false, session.getId(), call_path, user_ip);
			    System.out.println("<BR><BR>Manager Only");
			    session.setAttribute("count", "0");
			    out.println("<BR><BR>"+sbf);
			    show_mlink = false;
			}
		    }
		    else if(aid.startsWith("4")) {
			if(!admin) {
			    out.println(MyJavaScript.disableHistory());
			    StringBuffer sbf = (new TopBarBean()).getLoginForm(false, session.getId(), call_path, user_ip);
			    System.out.println("<BR><BR>admin Only");
			    session.setAttribute("count", "0");
			    out.println("<BR><BR>"+sbf);
			    show_mlink = false;
			}
		    }
		    else {
			System.out.println("MainBarServlet:Error:aid="+aid);
			show_mlink = false;
		    }
		}

		if(show_mlink) {
		    try { 
			request.setAttribute("achs",achs);
			System.out.println("MainBarServlet:"+mlink);
			RequestDispatcher rd = request.getRequestDispatcher(mlink);
			rd.include(request,response);
		    }
		    catch (ServletException e) {
			System.out.println("Error:MainBarServlet:106:mlink=["+mlink+"]Error="+e);
		    }
		    catch (Exception e) {
			System.out.println("Error:MainBarServlet:107:mlink=["+mlink+"]Error="+e);
		    }
		}
		else {
		    StringBuffer last_call = new StringBuffer("<input type='hidden' name='mid' value='"+mid+"'>");
		    String pid = request.getParameter("pid");
		    if(pid != null) {
			last_call.append("<input type='hidden' name='pid' value='"+pid+"'>");
		    }
		    session.setAttribute("last_call", last_call);
		}

		out.println("</td>");
		/*
		out.println("<td align=center valign=top style='BORDER: 1px solid #999999;' width=130>");
		out.println(right);
		out.println("</td>");
		*/
		out.println("</tr>");
		out.println("</table>");
	    }
	    else if(type.equals("2")) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("<CENTER>");
		sbf.append("<TABLE cellSpacing=0 cellPadding=0 width=100% height=100% border=1 style='font-size: 10pt'>");
		sbf.append("<TR><TD>");
		sbf.append("<IFRAME src="+mlink+" width='1000' height='1200' framespacing='1' border='1' frameborder='1'></IFRAME>");
		sbf.append("</TD></TR>");
		sbf.append("</TABLE>");
		sbf.append("</CENTER>");
		out.println(sbf);
		sbf = null;
	    }
	    else if(type.equals("3")) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("<HTML><HEAD>");
		sbf.append("<TITLE></TITLE>");
		sbf.append("</HEAD><BODY><CENTER>");
		sbf.append("<TABLE cellSpacing=0 cellPadding=1 width=100% height=100% border=0 style='font-size: 10pt'>");
		sbf.append("<TR><TD>");
		if(user_name == null || user_name.equals("guest")) {
		    sbf.append("<IFRAME src="+mlink+"/public width=100% height=100% framespacing='0' frameborder='1'>");
		    sbf.append("</IFRAME>");
		}
		else {
		    sbf.append("<IFRAME src="+mlink+"/"+user_name+" width=100% height=100% framespacing='0' frameborder='1'>");
		    sbf.append("</IFRAME>");
		}
		sbf.append("</TD></TR>");
		sbf.append("</TABLE>");
		sbf.append("</CENTER>");
		sbf.append("</BODY></HTML>");
		out.println(sbf);
		sbf = null;
	    }
	    else if(type.equals("5")) {
		out.println("<IFRAME src="+mlink+" width=100% height=100% framespacing='0' frameborder='1'>");
		out.println("</IFRAME>");
	    }
	    else {
		out.println(mlink);
	    }
	    out.println(MyXml.getComments("End Of Main"));
	}
	catch(IOException e) {
	    System.out.println("Error:MainBarServlet:107:"+e);
	    return;
	}
    }

    public void doGet(HttpServletRequest request,  HttpServletResponse response)
    {
	doPost(request, response);
    }
}






